package com.lwx.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisManage {
    private static JedisPool jedisPool;
    static {
        // 设置连接池配置
        String host = "192.168.56.201";
        int port = 6379;
         jedisPool =
            new JedisPool(host, port);
      }
    
    public static String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
          return jedis.set(key, value);
        } catch (Exception e) {
          e.printStackTrace();
          return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
      }
    
    public static String get(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
          return jedis.get(key);
        } catch (Exception e) {
          e.printStackTrace();
          return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
      }
    
    public static Long hset(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
          return jedis.hset(key, field, value);
        } finally {
            jedisPool.returnResource(jedis);
        }

      }
    
    public static String hget(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        try {
          return jedis.hget(key, field);
        } catch (Exception e) {
          e.printStackTrace();
          return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
      }
}
